/*********************************************************************************
 *      Copyright:  (C) 2022 Zhang Changxing<ZhangChangxingVIP@163.com>
 *                  All rights reserved.
 *
 *       Filename:  socket.c
 *    Description:  This file 
 *                 
 *        Version:  1.0.0(04/13/2022)
 *         Author:  Zhang Changxing <ZhangChangxingVIP@163.com>
 *      ChangeLog:  1, Release initial version on "04/13/2022 01:33:25 AM"
 *                 
 ********************************************************************************/

#include "socket.h"

int 	connect_flag = 0;
int 	init_flag = 0;

 
/*Create a socket and connect to the server*/
int connect_socket(int *sockfd, int port, char *ip_addr)
{
	int 				rv = -1;
	struct sockaddr_in 	servaddr;

	if((NULL == ip_addr)||(port < 0)||(NULL == sockfd))
	{
		log_error("The socket_init() argument incorrect!\n");
		return -1;
	}


	*sockfd = socket(AF_INET, SOCK_STREAM, 0);
	if ((*sockfd) < 0)
	{
		log_error("Create socket failure:%s\n", strerror(errno));
		return -2;
	}

	memset(&servaddr, 0 ,sizeof(servaddr));
	servaddr.sin_family = AF_INET;
	servaddr.sin_port = htons(port);
	servaddr.sin_addr.s_addr = inet_addr(ip_addr);
	
	rv = connect(*sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr));
	if (rv < 0)
	{
		if(init_flag == 0)
		{
			log_error("Connect server failure:%s\n", strerror(errno));
			init_flag = 1;
		}
		return -3;
	}

	init_flag = 0;
	return rv;
}

/* send buftok data to server */
int sendata(int sockfd, pack_info_t pack_info)
{
	int 	write_rv1 = -1;
	char 	sendata_buffer[100];
	int		left_bytes = 0;
	int		len = 0;

	if(sockfd < 0)
	{
		log_error("The sendata() argument connfd incorrect!\n");
		return -1;
	}

	memset(sendata_buffer, 0, sizeof(sendata_buffer));
	sprintf(sendata_buffer, "%s/%s/%f",  pack_info.devid, pack_info.strtime, pack_info.temper);

	left_bytes = strlen(sendata_buffer);
	log_debug("Sendata sendata_buffer:%s\n", sendata_buffer);
	while(left_bytes != 0)
	{
		write_rv1 = write(sockfd, sendata_buffer + len, left_bytes);
		if (write_rv1 < 0)
		{
			log_error("Send data to sever failure:%s\n", strerror(errno));
			return -1;
		}
		left_bytes -= write_rv1;
		len +=write_rv1; 
	}
	
	log_info("Send data to sever successfully:%s\n", sendata_buffer);
	return 0;
}


/*Real-time determine whether socket communication is disconnected*/
int get_sock_status(int sockfd)
{
    struct 	tcp_info info;
    int 	len = sizeof(info);
    
	if(sockfd < 0)
	{
		log_error("The sendata() argument connfd incorrect!\n");
		return -1;
	}

    getsockopt(sockfd, IPPROTO_TCP, TCP_INFO, &info, (socklen_t *) & len);
    if ((info.tcpi_state == 1)) 
	{
		if(connect_flag == 0)
        {
			log_info("Socket connected\n");
			connect_flag = 1;
		}
        return 0;
    } 
	else 
	{
		if(connect_flag == 0)
		{
        	log_error("Socket disconnected!\n");
			log_error("*************************************************************************\n");
			connect_flag = 1;
		}
        return -2;
    }
}

